requirejs.config({
    baseUrl: baseUrl + 'Public/js',
    paths: {
        jquery: 'jquery',
        validator: 'lib/validator/local/zh-CN',
        jqueryForm: 'lib/jquery.form',
        bootstrap: 'lib/bootstrap.min'
    },
    shim: {
        validator: [baseUrl + 'Public/js/lib/validator/jquery.validator.js?css']
    }
});

//表单事件
requirejs(['jquery', 'validator', 'jqueryForm'], function ($) {
    $('.form').on('focus', 'input', function () {
        var $this = $(this),
            $group = $this.parents('.form-group');

        $group.addClass('form-focus');
    }).on('blur', 'input', function () {
        var $this = $(this),
            $group = $this.parents('.form-group');

        $group.removeClass('form-focus');
    }).validator({
        timely: 2,
        bindClassTo: '.form-group',
        invalidClass: 'form-error',
        msgClass: 'form-error-message',
        msgIcon: '',
        ignore: ':hidden, :disabled',
        target: function (elem) {
            var $formitem = $(elem).closest('.form-group').find('label'),
                $msgbox = $formitem.find('span.msg-box');
            if (!$msgbox.length) {
                $msgbox = $('<span class="msg-box"></span>').appendTo($formitem);
            }
            return $msgbox;
        },
        validation: function (element, result) {
            var $btnSubmit = $(element.form).find(':submit');

            $btnSubmit.prop('disabled', !element.form.isValid);
        }
    });
});

requirejs(['jquery', 'bootstrap']);

define('prompt', ['jquery'], function ($) {
    var tpl = $('<div class="prompt"><span class="prompt-content"></span><span class="prompt-close icon-add rotate-45"></span></div>'),
        body = $('body'),
        clock = null,
        event = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';

    if (body.find('.prompt').length == 0) {
        tpl.appendTo(body);
    } else {
        tpl = body.find('.prompt');
    }

    var init = function (msg, time, callback) {
        time = time || 3000;
        callback = callback || function () { };

        clearTimeout(clock);

        tpl.removeClass('showed animated slideInDown slideOutUp prompt-warning prompt-error');
        tpl.find('.prompt-content').html(msg);
        tpl.addClass('showed animated slideInDown');

        clock = setTimeout(function () {
            close();
            callback();
        }, time);
    };

    var close = function () {
        tpl.on(event, function () {
            tpl.removeClass('showed animated slideInDown slideOutUp').off(event);
        }).addClass('animated slideOutUp');

        clearInterval(clock);
    };

    tpl.on('click', '.prompt-close', function () {
        close();
    });

    return {
        alert: function (msg, time, callback) {
            init(msg, time, callback);
        },
        warning: function (msg, time, callback) {
            init(msg, time, callback);

            tpl.addClass('prompt-warning');
        },
        error: function (msg, time, callback) {
            init(msg, time, callback);

            tpl.addClass('prompt-error');
        }
    };
});

//验证码获取
define('captcha', ['jquery'], function ($) {
    return {
        refresh: function () {
            var captchaUrl = '/captcha/generate',
                imgHtml = '<img src="'+captchaUrl+'" />';

            $('.captcha-box').html(imgHtml);
        }
    }
});